<?php


namespace App\Components\LoadBalance;


class RandLoadBalance implements LoadBalanceInterface
{
     public static function select(array $serviceList): array
     {
          $sum = 0;//总的权重值
          $weightList = [];
          foreach ($serviceList as $k => $v){
              $sum += $v['weight'];
              $weightList[$k] = $sum;
          }
          //print_r($weightList);
          $rand = mt_rand(0, $sum);
          //var_dump($rand);
          foreach ($weightList as $k => $v){
              if($rand <= $v){
                  return $serviceList[$k];
              }
          }
     }
}
